package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/7/9 22:44
 * @description：https://leetcode-cn.com/problems/find-majority-element-lcci/
 */
public class 主要元素 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public int majorityElement(int[] nums) {
            Map<Integer, Integer> map = new HashMap<>();
            int length = nums.length / 2;
            for (int num : nums) {
                map.put(num, map.getOrDefault(num, 0) + 1);
                if (map.get(num) > length) {
                    return num;
                }
            }
            return -1;
        }
    }

}
